<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>webgl着色器</title>
  <script src="lib/webgl-debug.js"></script>
  <script src="lib/webgl-utils.js"></script>
  <script src="lib/cuon-utils.js"></script>
  <script src="lib/cuon-matrix.js"></script>
</head>

<body onload="main()">
  <canvas id="canvas" width="800" height="800"></canvas>
</body>
<script>
  // 顶点着色器伪代码
  var vShder_source = `
  attribute vec4 position;
  attribute vec4 color;
  uniform mat4 mat;
  varying vec4 col ;
void main(){
gl_Position =mat* position;
col = color;
}
`
  //片元着色器伪代码
  var fShder_source = `
  precision mediump float;
  varying vec4 col;
void main(){
gl_FragColor = col; //设置颜色
}
`
  function main() {
    var canvas = document.getElementById('canvas');
    //  获取上下文对象
    var gl = getWebGLContext(canvas);
    //  初始化着色器
    if (!initShaders(gl, vShder_source, fShder_source)) {
      return;
    }

    var n = initVertexBuffers(gl);

    var mat = gl.getUniformLocation(gl.program, "mat");

    gl.clearColor(0, 0, 0, 1);
    draw(gl, n, mat);

    document.onkeydown = function (event) {
      keydown(event, gl, n, mat);
    }

  }

  var ex =0.20,ey =0.25,ez=0.25;

  function keydown(event, gl, n, mat) {
    var kc = event.keyCode;
    switch (kc) {
      case 39: ex += 0.1; break;
      case 37: ex -= 0.1; break;
      case 38: ey += 0.1; break;
      case 40: ey -= 0.1; break;
        return;
    }
    draw(gl, n, mat);
  }

  function draw(gl, n, mat) {
    // 设置视觉矩阵的相关信息（视点，视线，上方向）
    var viewMatrix = new Matrix4();
    viewMatrix.setLookAt(ex, ey, ez, 0, 0, 0, 0, 1, 0);

    //设置模型矩阵的相关信息
    var modelMatrix = new Matrix4();
    modelMatrix.setRotate(-10, 0, 0, 1);

    // 计算模型视图矩阵 viewMatrix.multiply(modelMatrix) 相当于在着色器里面u_ViewMatrix * u_ModelMatrix
    var modeViewMatrix = viewMatrix.multiply(modelMatrix);

    //将视图矩阵传给 着色器
    gl.uniformMatrix4fv(mat, false, modeViewMatrix.elements);
    gl.clear(gl.COLOR_BUFFER_BIT);
    gl.drawArrays(gl.TRIANGLES, 0, n);
  }

  function initVertexBuffers(gl) {
    var vertexDatas = new Float32Array([
      //顶点的坐标和颜色
      0.0, 0.5, -0.4, 0.4, 1.0, 0.4, // 绿色三角形，在最后面的三个点
      -0.5, -0.5, -0.4, 0.4, 1.0, 0.4,
      0.5, -0.5, -0.4, 1.0, 0.4, 0.4,

      0.5, 0.4, -0.2, 1.0, 0.4, 0.4, // 黄色三角形，在中间的三个点
      -0.5, 0.4, -0.2, 1.0, 1.0, 0.4,
      0.0, -0.6, -0.2, 1.0, 1.0, 0.4,

      0.0, 0.5, 0.0, 0.4, 0.4, 1.0,  // 蓝色三角形，在最前面的三个点
      -0.5, -0.5, 0.0, 0.4, 0.4, 1.0,
      0.5, -0.5, 0.0, 1.0, 0.4, 0.4
    ]);
    var vertexbuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, vertexbuffer);
    gl.bufferData(gl.ARRAY_BUFFER, vertexDatas, gl.STATIC_DRAW);

    var size = vertexDatas.BYTES_PER_ELEMENT;
    var position = gl.getAttribLocation(gl.program, "position");
    var color = gl.getAttribLocation(gl.program, "color");
    gl.vertexAttribPointer(position, 3, gl.FLOAT, false, size * 6, 0);
    gl.enableVertexAttribArray(position);

    gl.vertexAttribPointer(color, 3, gl.FLOAT, false, size * 6, size * 3);
    gl.enableVertexAttribArray(color);
    return 9;
  }

</script>

</html>